<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class TPS extends CI_Controller {
  public function __construct(){
    parent::__construct();
    $this->load->model('TPS_model','m');//load model dan singkat dengan m
    $this->load->helper('url');
  }

  
  public function index()
  {
    //load library table agar mudah membuat tabel
    $this->load->library('table');

    $all_TPS = $this->m->all();//return array of object
    foreach($all_TPS as $TPS){
      $this->table->add_row(
        $TPS->id,
        anchor('tps/view/'.$TPS->id,'Lihat'),
        anchor('tps/edit/'.$TPS->id,'Edit')
      );
    }
    echo $this->table->generate();
  }



  public function add()
  {
    //untuk memudahkan membuat form
    $this->load->helper('form');
    //load library table agar mudah membuat tabel
    $this->load->library('table');

    //buka form
    echo form_open('tps/save');

    $this->table->add_row('Nama',':',form_input('nama'));

    $options = $this->m->getAllKelurahan();
    $this->table->add_row('id_kelurahan',':',form_dropdown('id_kelurahan',$options));

    $this->table->add_row('latitude',':',form_input('latitude'));
    $this->table->add_row('longitude',':',form_input('longitude'));
    $this->table->add_row('zlevel',':',form_input('zlevel'));

    //tombol tambah
    $this->table->add_row(form_submit('tambah', 'Tambah'));
    $this->table->add_row(anchor('tps', 'Kembali'));
    echo $this->table->generate();

    //tutup form
    echo form_close();
  }

  //save our new TPS
  public function save()
  {
    //save 
    $data['nama']=$this->input->post('nama');
    $data['id_kelurahan']=(int)$this->input->post('id_kelurahan');
    $data['latitude']=$this->input->post('latitude');
    $data['longitude']=$this->input->post('longitude');
    $data['zlevel']=(int)$this->input->post('zlevel');

    //simpan id autoincrement yang dihasilkan untuk ditampilkan jika sukses disimpan
    $id = $this->m->insert($data);
		redirect('tps/view/'.$id);

  }

  public function view($id=1)
  {
    $this->load->library('table');
    $o = $this->m->get($id);
    $this->table->add_row('id',':', $o->id);
    $this->table->add_row('created_at',':', $o->created_at);
    $this->table->add_row('updated_at',':', $o->updated_at);
    $this->table->add_row('Nama',':',$o->nama);
    $this->table->add_row('Kelurahan',':',$o->id_kelurahan);
    $this->table->add_row('Latitude',':',$o->latitude);
    $this->table->add_row('Longitude',':',$o->longitude);
    $this->table->add_row('zLevel',':',$o->zlevel);
    $this->table->add_row(anchor('tps/add','Add'),anchor('tps/edit/'.$id,'Edit'));
    echo $this->table->generate();
  }

  public function edit($id)
  {
    $this->load->library('table');
    $this->load->helper('form');
    $o = $this->m->get($id);

    echo form_open('tps/update/'.$o->id);
    //push the table
    $this->table->add_row('id',':', $o->id);
    $this->table->add_row('created_at',':', $o->created_at);
    $this->table->add_row('updated_at',':', $o->updated_at);

    $this->table->add_row('Nama',':',form_input('nama',$o->nama));
    $options = $this->m->getAllKelurahan();

    $this->table->add_row('Kelurahan',':',form_dropdown('id_kelurahan',$options,$o->id_kelurahan));
    $this->table->add_row('Latitude',':',form_input('latitude',$o->latitude));
    $this->table->add_row('Longitude',':',form_input('longitude',$o->longitude));
    $this->table->add_row('zLevel',':',form_input('zlevel',$o->zlevel));

    $this->table->add_row(form_submit('update', 'Update'));

    //now render the table
    echo $this->table->generate();
    echo form_close();


  }

  public function update($id){
    //update
    $data['nama']=$this->input->post('nama');
    $data['id_kelurahan']=(int)$this->input->post('id_kelurahan');
    $data['latitude']=$this->input->post('latitude');
    $data['longitude']=$this->input->post('longitude');
    $data['zlevel']=(int)$this->input->post('zlevel');

    $this->m->update($id,$data);
		redirect('tps/view/'.$id);

  }

}
